*******************************************************************************
* This dofile creates tex-files for the tables in the paper
*******************************************************************************

* Load data
use "$data_prep/owwa_data_merged_long", clear
estimates clear


*******************************************************************************
* This section produces Tables 2, 3, 4, E.8, F.12

* Loop for the five different samples: all, HK, KSA, notalk, talk
forvalues i = 4(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	else if `i'==4 {
		local sample = "notalk"
		local j=1
	}
	else if `i'==3 {
		local sample = "talk"
		local j=0
	}	

	preserve

    * Restrict to respective sample
	if `i'<3 drop if ksa==`i'
	else if `i'>=3 drop if talk_employer==`j'
	
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .
	
    * Main outcomes (Table 2)
    regout std_treat_si i.type_int $controls3 talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(std_treat_si_w3_`sample')
    regout empl_rel_av_ i.type_int $controls talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(empl_rel_av__w3_`sample')
    regout status_3_c1 $controls i.type_int workabroad_m2yrs_1 mis_workabroad_yrs_1 if wave==3 & gift<., cluster(rno) treat(gift) estname(status_3_c1_`sample')
	regout child_ofw_3 $controls i.good_child_ofw_1 if wave==3 & gift<., cluster(rno) treat(gift) estname(gcofw_w3d_`sample')


    * Detailed treatment (Table 3)
    local treatdetail "noshouting_ noviolence_ nothreat_ nosexharass_ no_worksick_ enoughfood_ restday_ leavehouse_ sal_ontime_ sal_noded_ sparetime_"
	local varno 1
	foreach depvar of varlist `treatdetail' {
		regout `depvar' talk_employer $controls if wave==3 & direct_3==1 & gift<., cluster(rno) treat(gift) estname(t`varno'_w3d_`sample')
		local varno = `varno'+1
	}

	* Subjective employer assessment (Table F.12)
    local employerassess "i_nstd_employer i_empl_good_ i_noempl_short_ i_noempl_slaver_ i_noempl_violent_ i_noempl_strict_"
	foreach depvar of varlist `employerassess' {
		regout `depvar' $controls talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(`depvar'_w3_`sample')
	}

    * Household assessment (Tables 4 and E.8)
    local hhoutcome3 "hh_efftot_ hh_financial_3 hh_stdliving_3 hh_housing_3 hh_health_3 hh_educ_3 hh_famlife_3 hh_soclife_3"
    local hhoutcomebetter3 "b_hh_efftot_ b_hh_financial_ b_hh_stdliving_ b_hh_housing_ b_hh_health_ b_hh_educ_ b_hh_famlife_ b_hh_soclife_"
	foreach depvar in `hhoutcome3' `hhoutcomebetter3' {
		regout `depvar' $controls if wave==3 & gift<., cluster(rno) treat(gift) estname(`depvar'_w3_`sample')

		* Identify ordinal variables
		inspect `depvar'
		if r(N_unique)==3 {
			oprobit `depvar' gift $controls if wave==3 & gift<., cluster(rno)
			margins, dydx(gift) post
			estadd local depvar="`depvar'"
			estimates store `depvar'_w3_`sample'
		}	
	}



    restore
}

***** Export Table 2
esttab std_treat_si_w3_All empl_rel_av__w3_All status_3_c1_All  gcofw_w3d_All   ///
using "$outpath_tab/table2.tex", depvars compress  ///
$esttab_opt_stats_ols_p $cellsp  keep(gift  ) replace fragment ///
mtitles( "\shortstack{Index of \\treatment of DW \\(STE)}" ///
"\shortstack{With \\ initial \\ employer}"  ///
"\shortstack{DW: Good \\ for children\\ to become OFW}" ) ///
posthead("\midrule" "&\multicolumn{4}{c}{\textit{Full}}\\")  
        
esttab std_treat_si_w3_HK empl_rel_av__w3_HK status_3_c1_HK gcofw_w3d_HK ///
using "$outpath_tab/table2.tex", nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{4}{c}{\textit{Hong Kong}}\\")  

esttab std_treat_si_w3_KSA empl_rel_av__w3_KSA status_3_c1_KSA gcofw_w3d_KSA  ///
using "$outpath_tab/table2.tex", nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{4}{c}{\textit{Saudi Arabia}}\\")  	

esttab std_treat_si_w3_notalk empl_rel_av__w3_notalk status_3_c1_notalk gcofw_w3d_notalk ///
using "$outpath_tab/table2.tex", nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{4}{c}{\textit{Did not talk to employer}}\\")  

esttab std_treat_si_w3_talk empl_rel_av__w3_talk status_3_c1_talk gcofw_w3d_talk  ///
using "$outpath_tab/table2.tex", nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{4}{c}{\textit{Talked to employer}}\\")  	
	
***** Export Table 3
esttab t1_w3d_All t2_w3d_All t3_w3d_All t4_w3d_All t5_w3d_All t6_w3d_All t7_w3d_All t8_w3d_All t9_w3d_All t10_w3d_All t11_w3d_All ///
using "$outpath_tab/table3.tex", depvars compress  ///
$esttab_opt_stats_ols_p $cellsp  keep(gift  ) replace fragment ///
mtitles( "\shortstack{No\\shouting}" "\shortstack{No\\violence}" "\shortstack{No\\threats}" ///
"\shortstack{No sexual\\harassment}" "\shortstack{No work\\when sick}" "\shortstack{Have\\enough\\food}" "\shortstack{Weekly\\rest day}" /// 
"\shortstack{Leave the\\employer's\\house}" "\shortstack{Salary\\on time}"  ///
"\shortstack{No salary\\deduction}" "\shortstack{Spare time\\(hours)}") ///
posthead("\midrule" "&\multicolumn{11}{c}{\textit{Full}}\\")  
        
esttab t1_w3d_HK t2_w3d_HK t3_w3d_HK t4_w3d_HK t5_w3d_HK t6_w3d_HK t7_w3d_HK t8_w3d_HK t9_w3d_HK t10_w3d_HK t11_w3d_HK ///
using "$outpath_tab/table3.tex",nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{11}{c}{\textit{Hong Kong}}\\")  

esttab  t1_w3d_KSA t2_w3d_KSA t3_w3d_KSA t4_w3d_KSA t5_w3d_KSA t6_w3d_KSA t7_w3d_KSA t8_w3d_KSA t9_w3d_KSA t10_w3d_KSA t11_w3d_KSA ///
using "$outpath_tab/table3.tex",nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{11}{c}{\textit{Saudi Arabia}}\\")  	

esttab t1_w3d_notalk t2_w3d_notalk t3_w3d_notalk t4_w3d_notalk t5_w3d_notalk t6_w3d_notalk t7_w3d_notalk t8_w3d_notalk t9_w3d_notalk t10_w3d_notalk t11_w3d_notalk ///
using "$outpath_tab/table3.tex",nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{11}{c}{\textit{Did not talk to employer}}\\")  

esttab  t1_w3d_talk t2_w3d_talk t3_w3d_talk t4_w3d_talk t5_w3d_talk t6_w3d_talk t7_w3d_talk t8_w3d_talk t9_w3d_talk t10_w3d_talk t11_w3d_talk ///
using "$outpath_tab/table3.tex",nonumbers compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
posthead("" "&\multicolumn{11}{c}{\textit{Talked to employer}}\\")  

**** Export Table 4
esttab hh_efftot__w3_All hh_financial_3_w3_All hh_stdliving_3_w3_All ///
hh_housing_3_w3_All hh_health_3_w3_All hh_educ_3_w3_All hh_famlife_3_w3_All ///
hh_soclife_3_w3_All using "$outpath_tab/gift_spillovers.tex", nogaps ///
$esttab_opt_stats_ols_p $cellsp keep(gift 1._predict 2._predict 3._predict) replace fragment width(10cm) ///
mtitles("\shortstack{HH:\\Overall\\effect}" ///
"\shortstack{HH: Effect\\on financial\\security}" "\shortstack{HH: Effect on\\standard of\\living}" ///
"\shortstack{HH: Effect\\on housing}" "\shortstack{HH: Effect\\on health}" ///
"\shortstack{HH: Effect\\on education}" "\shortstack{HH: Effect\\on family\\life}" "\shortstack{HH: Effect\\on social\\life}") ///
posthead("\midrule" "&\multicolumn{8}{c}{\textit{Full}}\\") coeflabels(1._predict Worse 2._predict Same 3._predict Better)
        
esttab hh_efftot__w3_HK hh_financial_3_w3_HK hh_stdliving_3_w3_HK ///
hh_housing_3_w3_HK hh_health_3_w3_HK hh_educ_3_w3_HK hh_famlife_3_w3_HK ///
hh_soclife_3_w3_HK using "$outpath_tab/gift_spillovers.tex", nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift 1._predict 2._predict 3._predict) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Hong Kong}}\\") coeflabels(1._predict Worse 2._predict Same 3._predict Better)

esttab hh_efftot__w3_KSA hh_financial_3_w3_KSA hh_stdliving_3_w3_KSA ///
hh_housing_3_w3_KSA hh_health_3_w3_KSA hh_educ_3_w3_KSA hh_famlife_3_w3_KSA ///
hh_soclife_3_w3_KSA	using "$outpath_tab/gift_spillovers.tex",nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift 1._predict 2._predict 3._predict) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Saudi Arabia}}\\") coeflabels(1._predict Worse 2._predict Same 3._predict Better)	

esttab hh_efftot__w3_notalk hh_financial_3_w3_notalk hh_stdliving_3_w3_notalk ///
hh_housing_3_w3_notalk hh_health_3_w3_notalk hh_educ_3_w3_notalk hh_famlife_3_w3_notalk ///
hh_soclife_3_w3_notalk using "$outpath_tab/gift_spillovers.tex", nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift 1._predict 2._predict 3._predict) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Did not talk to employer}}\\") coeflabels(1._predict Worse 2._predict Same 3._predict Better)

esttab hh_efftot__w3_talk hh_financial_3_w3_talk hh_stdliving_3_w3_talk ///
hh_housing_3_w3_talk hh_health_3_w3_talk hh_educ_3_w3_talk hh_famlife_3_w3_talk ///
hh_soclife_3_w3_talk	using "$outpath_tab/gift_spillovers.tex",nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift 1._predict 2._predict 3._predict) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Talked to employer}}\\") coeflabels(1._predict Worse 2._predict Same 3._predict Better)	

filefilter "$outpath_tab/gift_spillovers.tex" "$outpath_tab/table4.tex", ///
from("main                &            &            &            &            &            &            &            &            \BS\BS") to("") replace

**** Export Table E.8
esttab b_hh_efftot__w3_All b_hh_financial__w3_All b_hh_stdliving__w3_All ///
b_hh_housing__w3_All b_hh_health__w3_All b_hh_educ__w3_All b_hh_famlife__w3_All ///
b_hh_soclife__w3_All using "$outpath_tab/table_e8.tex", nogaps ///
$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment width(10cm) ///
mtitles("\shortstack{HH:\\Overall\\effect}" ///
"\shortstack{HH: Effect\\on financial\\security}" "\shortstack{HH: Effect on\\standard of\\living}" ///
"\shortstack{HH: Effect\\on housing}" "\shortstack{HH: Effect\\on health}" ///
"\shortstack{HH: Effect\\on education}" "\shortstack{HH: Effect\\on family\\life}" "\shortstack{HH: Effect\\on social\\life}") ///
posthead("\midrule" "&\multicolumn{8}{c}{\textit{Full}}\\")
        
esttab b_hh_efftot__w3_HK b_hh_financial__w3_HK b_hh_stdliving__w3_HK ///
b_hh_housing__w3_HK b_hh_health__w3_HK b_hh_educ__w3_HK b_hh_famlife__w3_HK ///
b_hh_soclife__w3_HK using "$outpath_tab/table_e8.tex", nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Hong Kong}}\\") 

esttab b_hh_efftot__w3_KSA b_hh_financial__w3_KSA b_hh_stdliving__w3_KSA ///
b_hh_housing__w3_KSA b_hh_health__w3_KSA b_hh_educ__w3_KSA b_hh_famlife__w3_KSA ///
b_hh_soclife__w3_KSA using "$outpath_tab/table_e8.tex", nonumbers ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
posthead("" "&\multicolumn{8}{c}{\textit{Saudi Arabia}}\\")
	
erase "$outpath_tab/gift_spillovers.tex"

**** Export Table F.12
esttab i_nstd_employer_w3_All i_empl_good__w3_All i_noempl_short__w3_All i_noempl_slaver__w3_All  ///
i_noempl_violent__w3_All i_noempl_strict__w3_All  ///
using "$outpath_tab/table_f12.tex", depvars compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
mtitles("\shortstack{Average \\employer \\description}" "\shortstack{Employer\\good}" ///
"\shortstack{Employer\\no \\short-temper}" "\shortstack{Employer\\no\\slaver}" ///
"\shortstack{Employer\\not\\violent}" "\shortstack{Employer\\not\\strict}" ) ///
posthead("\midrule" "&\multicolumn{6}{c}{\textit{Full}}\\" ) 

esttab i_nstd_employer_w3_HK i_empl_good__w3_HK i_noempl_short__w3_HK i_noempl_slaver__w3_HK  ///
i_noempl_violent__w3_HK i_noempl_strict__w3_HK  ///
using "$outpath_tab/table_f12.tex", compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nonumbers nomtitles fragment ///
posthead("" "&\multicolumn{6}{c}{\textit{Hong Kong}}\\") 

esttab i_nstd_employer_w3_KSA i_empl_good__w3_KSA i_noempl_short__w3_KSA i_noempl_slaver__w3_KSA  ///
i_noempl_violent__w3_KSA i_noempl_strict__w3_KSA ///
using "$outpath_tab/table_f12.tex", compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nonumbers nomtitles fragment ///
posthead("" "&\multicolumn{6}{c}{\textit{Saudi Arabia}}\\") 	

esttab i_nstd_employer_w3_notalk i_empl_good__w3_notalk i_noempl_short__w3_notalk i_noempl_slaver__w3_notalk  ///
i_noempl_violent__w3_notalk i_noempl_strict__w3_notalk  ///
using "$outpath_tab/table_f12.tex", compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nonumbers nomtitles fragment ///
posthead("" "&\multicolumn{6}{c}{\textit{Did not talk to employer}}\\") 

esttab i_nstd_employer_w3_talk i_empl_good__w3_talk i_noempl_short__w3_talk i_noempl_slaver__w3_talk  ///
i_noempl_violent__w3_talk i_noempl_strict__w3_talk ///
using "$outpath_tab/table_f12.tex", compress ///
$esttab_opt_stats_ols_p $cellsp keep(gift) append nonumbers nomtitles fragment ///
posthead("" "&\multicolumn{6}{c}{\textit{Talked to employer}}\\") 	
	

estimates clear

*******************************************************************************
*** Multiple hypothesis tests for aggregate index
* Results reported in tablenote of Table 2

	preserve
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .
	keep std_treat_si gift $controls li_duration_ wave talk_employer
		
	local options "bootstrap(10000) robust seed(83)"
	
	mhtreg (std_treat_si gift $controls3 talk_employer if ksa==0 & gift<. & wave==3) (std_treat_si gift $controls3 talk_employer if ksa==1 & gift<. & wave==3), `options'
	
	matrix mhtres = r(results)
	
	restore
	
	matrix rownames mhtres = "hk" "ksa"
	
	esttab matrix(mhtres) using "$outpath_tab/table2_mht.tex", replace fragment
	
*******************************************************************************
* This section produces Tables E.2

* Loop for the five different samples: all, HK, KSA, notalk, talk
forvalues i = 2(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"

	preserve

    * Restrict to respective sample
	drop if ksa==`i'
	
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .
	

    *** Remember intervention (Table E.2)
    local gift "remember_gift_ gave_gift_"
	local varno 1
	foreach depvar of varlist `gift' {
	regout `depvar' $controls if wave==3 & direct_3==1 & gift<., cluster(rno) treat(gift) estname(m`varno'_w3d_`sample')
	local varno = `varno'+1
	}



    restore
}

    * Export Table E.2
	esttab m1_w3d_All m2_w3d_All ///
	using "$outpath_tab/table_e2.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles( "Remembers assignment" "Followed instructions") ///
	posthead("\midrule" "&\multicolumn{2}{c}{\textit{Full}}\\") 
	
	esttab m1_w3d_HK m2_w3d_HK ///
	using "$outpath_tab/table_e2.tex", nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{2}{c}{\textit{Hong Kong}}\\") 

	esttab m1_w3d_KSA m2_w3d_KSA ///
	using "$outpath_tab/table_e2.tex", nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{2}{c}{\textit{Saudi Arabia}}\\") 
		
estimates clear

*******************************************************************************
* This section produces Tables E.3 and E.4

* Loop for the three different samples: all, HK, KSA
forvalues i = 2(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"

	preserve

    * Restrict to respective sample
	drop if ksa==`i'

    local attrition "proxy_3 direct_3 directorproxy_3 successint_hh_3" 

    * Attrition test (Table E.3)
	foreach var of varlist `attrition' { 
		regout `var' $controls_att if wave==1 & gift<., cluster(rno) treat(gift) estname(`var'_w3_`sample')
    }

	*** Attrition test with interactions (Table E.4)
	local interactions ""
	foreach item in $controls_att {
		gen ia_`item' = gift*`item'
		local item_label : variable label `item'
		label var ia_`item' " x `item_label'"
		
		local interactions "`interactions' ia_`item'"
	}

	foreach var of varlist `attrition' { 
		regress `var' gift `interactions' $controls_att if wave==1 & gift<., cluster(rno)
		eststo att_`var'_`sample'
		testparm gift `interactions'
		estadd scalar ia_pval = r(p)
	}

    restore
}

    * Export Table E.3
	esttab directorproxy_3_w3_All direct_3_w3_All proxy_3_w3_All successint_hh_3_w3_All ///
    using "$outpath_tab/table_e3.tex", ///
	$esttab_opt_stats_ols $cellsp keep(gift) replace fragment ///
	mgroups("Successful re-interview with DW" "... with household", pattern(1 0 0 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles( "\shortstack{Direct or \\ proxy}" "Direct" "Proxy" ) ///
	posthead("\midrule" "&\multicolumn{4}{c}{\textit{Full}}\\") 
		 
	esttab  directorproxy_3_w3_HK direct_3_w3_HK proxy_3_w3_HK successint_hh_3_w3_HK ///
    using "$outpath_tab/table_e3.tex", nonumbers ///
	$esttab_opt_stats_ols $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{4}{c}{\textit{Hong Kong}}\\") 
	
	esttab  directorproxy_3_w3_KSA direct_3_w3_KSA proxy_3_w3_KSA successint_hh_3_w3_KSA ///
    using "$outpath_tab/table_e3.tex", nonumbers ///
	$esttab_opt_stats_ols $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{4}{c}{\textit{Saudi Arabia}}\\") 	
	
    * Export Table E.4
	esttab att_directorproxy_3_All att_direct_3_All att_proxy_3_All att_successint_hh_3_All ///
    using "$outpath_tab/table_e4.tex", ///
	$esttab_opt_stats_ols_f $cellsp keep(gift ia*) replace fragment ///
	b(3) ci(2) wide ///
	mgroups("Successful re-interview with DW" "... with household", pattern(1 0 0 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles( "\shortstack{Direct or \\ proxy}" "Direct" "Proxy") ///
	posthead("\midrule" "&\multicolumn{8}{c}{\textit{Full}}\\") 
		 
	esttab  att_directorproxy_3_HK att_direct_3_HK att_proxy_3_HK att_successint_hh_3_HK ///
    using "$outpath_tab/table_e4.tex", nonumbers ///
	$esttab_opt_stats_ols_f $cellsp  keep(gift ia*) drop(ia_ksa ia_sharing) append nomtitles fragment ///
	b(3) ci(2) wide ///
	posthead("" "&\multicolumn{8}{c}{\textit{Hong Kong}}\\") 
	
	esttab  att_directorproxy_3_KSA att_direct_3_KSA att_proxy_3_KSA att_successint_hh_3_KSA ///
    using "$outpath_tab/table_e4.tex", nonumbers ///
	$esttab_opt_stats_ols_f $cellsp keep(gift ia*) drop(ia_ksa) append nomtitles fragment ///
	b(3) ci(2) wide ///
	posthead("" "&\multicolumn{8}{c}{\textit{Saudi Arabia}}\\") 	
		
estimates clear

*******************************************************************************
* This section produces Tables E.5 and E.7

* Loop for the three different samples: all, HK, KSA
local sample = "ksa"
local all = "2"
local ksa = "0"
local hk = "1"
forvalues i = 2(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	display "`sample'"

	preserve
	
	drop if ksa==`i'
	tab ksa
	
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .
	
    * Correlation between treatment of DW and wellbeing (Table E.5)
	reg s_mental_health_3 std_treat ksa mental_health_1 if wave==3, cluster(rno)
	estimates store mh_streat3_`sample'

	reg  std_treat hh_efftot ksa direct_3 if wave==3, cluster(rno)
	estimates store hheff_streat_p_`sample'	
	
	reg  std_treat hh_efftot ksa if wave==3 & direct_3==1, cluster(rno)
	estimates store hheff_streat_d_`sample'

    * HH remembers gift (Table E.7)
	regout hh_remember_gift $controls3 if wave==3 & gift<., cluster(rno) treat(gift) estname(hh_rem_w3_`sample')

	restore
	
}

* Export Table E.5
    esttab mh_streat3_All mh_streat3_HK mh_streat3_KSA ///
    using "$outpath_tab/table_e5.tex", ///
    $esttab_opt_stats $cellsp keep(std_treat) replace fragment   ///
    mtitles( "Full" "Hong Kong" "Saudi Arabia")  ///
    coeflabels(std_treat "Index of treatment of DW (STE)") 

* Export Table E.7
    esttab hheff_streat_p_All  hheff_streat_d_All hh_rem_w3_All   ///
    using "$outpath_tab/table_e7.tex", compress ///
    $esttab_opt_stats_ols_p $cellsp keep(hh_efftot_ gift ) replace fragment ///
    mtitles( "\shortstack{Index of treatment \\ of DW (STE), \\ all interviews}" "\shortstack{Index of treatment \\ of DW (STE), \\ direct interviews only}" "\shortstack{Aware \\ of \\ intervention}" ) ///
    posthead("\midrule" "&\multicolumn{3}{c}{\textit{Full}}\\") coeflabels(hh_efftot_ "HH: Overall effect") 
            
    esttab hheff_streat_p_HK hheff_streat_d_HK hh_rem_w3_HK  ///
    using "$outpath_tab/table_e7.tex", nonumbers ///
    $esttab_opt_stats_ols_p $cellsp keep(hh_efftot_ gift) append nomtitles fragment ///
    posthead("" "&\multicolumn{3}{c}{\textit{Hong Kong}}\\") coeflabels(hh_efftot_ "HH: Overall effect") 

    esttab hheff_streat_p_KSA  hheff_streat_d_KSA hh_rem_w3_KSA  ///
    using "$outpath_tab/table_e7.tex", nonumbers ///
    $esttab_opt_stats_ols_p $cellsp keep(hh_efftot_ gift) append nomtitles fragment ///
    posthead("" "&\multicolumn{3}{c}{\textit{Saudi Arabia}}\\") coeflabels(hh_efftot_ "HH: Overall effect") 	

estimates clear

*******************************************************************************
* This section produces Tables E.6, F.13

gen amount_received_3 = hh_g7_amount_receive_money_con_3
replace amount_received_3 = 0 if hh_g5_freq_remit_3==6
label var amount_received_3 "Amount of Remittances Received Last Time"

gen l_amount_received_3 = log(1+amount_received_3)
label var l_amount_received_3 "Log of Amount of Remittances Received Last Time"

gen amount_received_hsw = h_e14_amount_send_money_con_3
replace amount_received_h = 0 if h_e12_freq_remit_3==5
label var amount_received_h "Amount of Remittances Sent Last Time"

gen l_amount_received_hsw=log(1+amount_received_hsw)
label var l_amount_received_hsw "Log of Amount of Remittances Sent Last Time "

gen freq_remit_hsw = freq_remit_
replace freq_remit_hsw = 0 if h_e12_freq_remit_3==5
label var freq_remit_hsw "Frequency of Remittances DW"

gen freq_remit_3_hh = hh_freq_remit_3
replace freq_remit_3_hh = 0 if hh_g5_freq_remit_3==6
label var freq_remit_3_hh "Frequency of Remittances"

gen amount_received_2 = amount_remit_ if wave==2
replace amount_received_2 = 0 if  hh_d9_freq_send_money_2==5 &  wave==2
label var amount_received_2 "Amount of Remittance Received Last Time in Wave 2" 

* Gen variance of remittances variables
egen variance_remit = rowsd(amount_received_hsw amount_received_3 amount_received_2)  if amount_received_hsw<. & amount_received_3<. & amount_received_2<.
label var variance_remit "Variance of Remittances"
gen log_var = ln(1+variance_remit)

* Loop for the three different samples: all, HK, KSA
local sample = "ksa"
local all = "2"
local ksa = "0"
local hk = "1"
forvalues i = 2(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	display "`sample'"

	preserve
	
	drop if ksa==`i'
	tab ksa
	
	* Drop DWs that never went to HK/KSA
	keep if status_3 < 5 | status_3 >= .

	* Standardized treatment effect
	regout std_treat_si i.type_int $controls3 talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(std_treat_si_w3_`sample')
	regout std_treat_si ksa if wave==3 & gift<., cluster(rno) treat(gift) estname(std_treat_si_w3nc_`sample')
	regout std_treat $controls talk_employer if wave==3 & direct_3==1 & gift<., cluster(rno) treat(gift) estname(std_treat_w3d_`sample')
	
	* Standardized treatment effect only for cases where TNS records match the assignment list
	gen h_gift = gift
	replace gift = . if gift!=gift_tns
	regout std_treat_si $controls3 talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(std_treat_si_aw3_`sample')
	replace gift = h_gift
	drop h_gift
	
	* IV estimates (column 5, Table E.6)
	rename gift gift2
	rename gave_gift gift
	foreach depvar of varlist std_treat {		
		ivregress 2sls `depvar' (gift=gift2) $controls if wave==3 & direct_3==1 & gift2<., cluster(rno)
		eststo `depvar'_iv_w3d_`sample'
	rename gift gave_gift
	rename gift2 gift
	}

    * Remittances (Table E.9)
    regout freq_remit_hsw $controls household_size_1 i.wave if gift<. & wave==3, cluster(cluster) treat(gift) estname(freq_remit_hsw_`sample')
    regout freq_remit_3_hh $controls household_size_1 i.wave if gift<. & wave==3, cluster(cluster) treat(gift) estname(freq_remit_3_hh_`sample')
	regout l_amount_received_hsw $controls household_size_1 i.wave if gift<. & wave==3, cluster(cluster) treat(gift) estname(l_amount_received_hsw_`sample')
	regout l_amount_received_3  $controls household_size_1 i.wave if gift<. & wave==3, cluster(cluster) treat(gift) estname(l_amount_received_3_`sample')
	regout log_var  $controls i.wave if gift<. & wave==2 & direct_3==1, cluster(cluster) treat(gift) estname(log_var_`sample')

    * HH respondent OFW intentions (Table F.13)
	regout hh_intent_ofw_3 $controls if wave==3 & gift<., cluster(rno) treat(gift) estname(intend_ofw_w3h_`sample')

	* Effects on communication with hh (Table F.14)
    local comm "std_comm call_length_ text_freq_ call_freq_"
	foreach depvar of varlist `comm' {
		regout `depvar' i.comm_fam_1 $controls if wave==2 & gift<., cluster(rno) treat(gift) estname(`depvar'_w2_`sample')

	}
	regout conf_phone_3 $controls talk_employer if wave==3 & gift<., cluster(rno) treat(gift) estname(conf_phone_3_w3_`sample')

	* Wellbeing (Table F.15)
    local welloutcome "std_well mental_health_3 migrant_well_3 happy_3 nervous_3 downheart_3 pain_3 homesick_3 overwhelm_3"
	foreach depvar of varlist `welloutcome' {
		regout `depvar' $controls mental_health_1 if wave==3 & direct_3==1 & gift<., cluster(rno) treat(gift) estname(`depvar'_w3d_`sample')
	}


    restore

}


* Export Table E.6
	esttab std_treat_si_w3_All std_treat_si_w3nc_All std_treat_si_aw3_All std_treat_w3d_All std_treat_iv_w3d_All ///
	using "$outpath_tab/table_e6.tex", depvars compress  ///
	$esttab_opt_stats_ols_p $cellsp  keep(gift  ) replace fragment ///
	mtitles( "\shortstack{Baseline \\ full sample}"  "\shortstack{No\\ controls}" "\shortstack{No assignment\\deviations}" "\shortstack{Direct \\  interviews}" "\shortstack{IV direct\\ interviews}"  ///
	"\shortstack{DW: good for children\\ to become OFW}" ) ///
	posthead("\midrule" "&\multicolumn{5}{c}{\textit{Full}}\\")  
		 
	esttab std_treat_si_w3_HK std_treat_si_w3nc_HK std_treat_si_aw3_HK std_treat_w3d_HK std_treat_iv_w3d_HK ///
	using "$outpath_tab/table_e6.tex", nonumbers compress ///
	$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Hong Kong}}\\")  
	
	esttab std_treat_si_w3_KSA std_treat_si_w3nc_KSA std_treat_si_aw3_KSA std_treat_w3d_KSA std_treat_iv_w3d_KSA ///
	using "$outpath_tab/table_e6.tex", nonumbers compress ///
	$esttab_opt_stats_ols_p $cellsp keep(gift  ) append nomtitles fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Saudi Arabia}}\\")  	

* Export Table E.9
	esttab l_amount_received_3_All l_amount_received_hsw_All freq_remit_3_hh_All freq_remit_hsw_All log_var_All ///
	using "$outpath_tab/table_e9.tex", depvars compress ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles("\shortstack{Amount received \\ by HH\\ (log)}" "\shortstack{Amount sent \\ by DW\\ (log)}" ///
	"\shortstack{Remittances \\ frequency, \\ reported by HH}" "\shortstack{Remittances \\ frequency, \\ reported by DW}" "\shortstack{Remittances \\ variance (log)}" )  ///
	posthead("\midrule" "&\multicolumn{5}{c}{\textit{Full}}\\") 
				
	esttab l_amount_received_3_HK l_amount_received_hsw_HK freq_remit_3_hh_HK freq_remit_hsw_HK log_var_HK ///
	using "$outpath_tab/table_e9.tex", compress nonumbers ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Hong Kong}}\\")  
			
	esttab l_amount_received_3_KSA l_amount_received_hsw_KSA freq_remit_3_hh_KSA freq_remit_hsw_KSA log_var_KSA ///
	using "$outpath_tab/table_e9.tex", compress nonumbers  ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Saudi Arabia}}\\") 

* Export Table F.13
	esttab intend_ofw_w3h_All intend_ofw_w3h_HK intend_ofw_w3h_KSA ///
	using "$outpath_tab/table_f13.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles("Full" "Hong Kong" "Saudi Ariabia") 


* Export Table F.14
	esttab  std_comm_w2_All text_freq__w2_All call_freq__w2_All call_length__w2_All conf_phone_3_w3_All ///
	using "$outpath_tab/table_f14.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles("\shortstack{Communication \\ index (STE)}" "\shortstack{Text\\ frequency}" ///
	"\shortstack{Call\\ frequency}" "\shortstack{Call\\ length}" "\shortstack{Confiscated\\phone}") ///
	posthead("\midrule" "&\multicolumn{5}{c}{\textit{Full}}\\") 
		 
	esttab  std_comm_w2_HK text_freq__w2_HK call_freq__w2_HK call_length__w2_HK conf_phone_3_w3_HK  ///
	using "$outpath_tab/table_f14.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles nonumbers fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Hong Kong}}\\") 
	
	esttab std_comm_w2_KSA text_freq__w2_KSA call_freq__w2_KSA call_length__w2_KSA conf_phone_3_w3_KSA  ///
	using "$outpath_tab/table_f14.tex", ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles nonumbers fragment ///
	posthead("" "&\multicolumn{5}{c}{\textit{Saudi Arabia}}\\") 				
	
		
* Export Table F.15
	esttab std_well_w3d_All mental_health_3_w3d_All  ///
	happy_3_w3d_All nervous_3_w3d_All downheart_3_w3d_All pain_3_w3d_All migrant_well_3_w3d_All ///
	homesick_3_w3d_All overwhelm_3_w3d_All  ///      
	using "$outpath_tab/table_f15.tex", depvars  ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) replace fragment ///
	mtitles( "\shortstack{Wellbeing \\ index (STE)}" "\shortstack{Mental\\health\\index}" ///
    "Happy" "Nervous" "\shortstack{Downhearted\\and blue}" "Pain" "\shortstack{Migrant\\specific\\well-being}" ///
	"Homesick" "\shortstack{Over-\\whelmed}") ///
	posthead("\midrule" "&\multicolumn{9}{c}{\textit{Full}}\\") 

	esttab std_well_w3d_HK mental_health_3_w3d_HK  ///
	happy_3_w3d_HK nervous_3_w3d_HK downheart_3_w3d_HK pain_3_w3d_HK migrant_well_3_w3d_HK ///
	homesick_3_w3d_HK overwhelm_3_w3d_HK ///
	using "$outpath_tab/table_f15.tex",  ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles nonumbers fragment ///
	posthead("" "&\multicolumn{9}{c}{\textit{Hong Kong}}\\") 

	esttab std_well_w3d_KSA mental_health_3_w3d_KSA  ///
	happy_3_w3d_KSA nervous_3_w3d_KSA downheart_3_w3d_KSA pain_3_w3d_KSA migrant_well_3_w3d_KSA ///
	homesick_3_w3d_KSA overwhelm_3_w3d_KSA ///
	using "$outpath_tab/table_f15.tex",  ///
	$esttab_opt_stats_ols_p $cellsp keep(gift) append nomtitles nonumbers fragment ///
	posthead("" "&\multicolumn{9}{c}{\textit{Saudi Arabia}}\\")	


estimates clear
